package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.Product;
import ebedynky.beans.ProductGenus;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 * @author Ondrej David (davidon1)
 */
public class ProductGenusService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieves the session (connection).
     */
    public ProductGenusService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds ProductGenus to the db.
     * @param gen ProductGenus to be inserted
     * @return ID of the added ProductGenus
     */
    public int addProductGenus(ProductGenus gen) {
        session.beginTransaction();
        int id = getMaxProductGenusID() + 1;
        gen.setProductGenusID(id);
        id = (Integer) session.save(gen);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of ProductGenuses.
     * @return Max ID
     */
    public int getMaxProductGenusID() {
        String hql = "select max(p.productGenusID) from ProductGenus p";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxgenid = (Integer) list.get(0);
            return maxgenid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a ProductGenus identified by the ID.
     * @param id of the ProductGenus
     * @return ProductGenus with the ID given (null if not found)
     */
    public ProductGenus getProductGenusByID(int id) {
        session.beginTransaction();
        ProductGenus gen = (ProductGenus) session.get(ProductGenus.class, new Integer(id));
        session.getTransaction().commit();
        return (gen);
    }

    /**
     * Retrieves a list of all ProductGenuses stored in the db.
     * @return List of all ProductGenuses (null if no ProductGenuses)
     */
    public List<ProductGenus> getAllProductGenuses() {
        session.beginTransaction();
        List<ProductGenus> genList = session.createQuery("from ProductGenus").list();
        session.getTransaction().commit();
        return (genList);
    }

    /**
     * Updates a specified ProductGenus identified by ID.
     * @param newGen an updated ProductGenus
     */
    public void updateProductGenus(ProductGenus newGen) {
        session.beginTransaction();
        ProductGenus gen = (ProductGenus) session.get(Product.class, new Integer(newGen.getProductGenusID()));
        gen.setProductGenusID(newGen.getProductGenusID());
        gen.setGenus(newGen.getGenus());

        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the ProductGenus from db.
     * @param id ProductGenus ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteProductGenus(int id) {
        try {
            session.beginTransaction();
            ProductGenus gen = (ProductGenus) session.get(ProductGenus.class, new Integer(id));
            session.delete(gen);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }

    /**
     * Returns a list containing ProductGenuses ordered by name.
     * @param name of the ProductGenus to be selected
     * @return List of ProductGenuses, List containing genus with match, null if ProductGenus not found
     */
    public List<ProductGenus> getProductGenusByName(String name) {
        session.beginTransaction();
        List<ProductGenus> gen = null;
        if (name.equals("") || name == null) {
            gen = session.createCriteria(ProductGenus.class).addOrder(Order.asc("genus")).list();
        } else {
            gen = session.createCriteria(ProductGenus.class).add(Restrictions.like("genus", "%" + name + "%")).addOrder(Order.asc("genus")).list();
        }
        session.getTransaction().commit();
        return (gen);
    }
}
